package com.android.clinic.app.ui.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.utils.Utils;

/**
 * Created by cheng on 2018/2/5.
 * 修改昵称
 */
public class UpdateActivity extends BaseActivity {


    public static final int NAME_CODE = 995;
    public static final int CARD_NUMBER_CODE = 996;
    public static final int OCCUPATION_CODE = 997;

    private String type;

    private EditText etContent;
    private ImageView ivClear;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_update_nick_name;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        type = this.getIntent().getStringExtra("type");
        String nickName = this.getIntent().getStringExtra("realName");
        String occupation = this.getIntent().getStringExtra("occupation");
        String cardNumber = this.getIntent().getStringExtra("cardNumber");

        etContent = (EditText) findViewById(R.id.et_update);

        switch (type) {
            case "0":
                setTitleBarTitle("姓名");
                if (!TextUtils.isEmpty(nickName) && !nickName.equals("请填写")) {
                    etContent.setText(nickName);
                } else {
                    etContent.setHint("请输入姓名");
                }
                break;
            case "1":
                setTitleBarTitle("身份证件号");
                if (!TextUtils.isEmpty(cardNumber) && !cardNumber.equals("请填写")) {
                    etContent.setText(cardNumber);
                } else {
                    etContent.setHint("请输入身份证件号");
                }
                break;
            case "2":
                setTitleBarTitle("职业");
                if (!TextUtils.isEmpty(occupation) && !occupation.equals("请填写")) {
                    etContent.setText(occupation);
                } else {
                    etContent.setHint("请输入职业");
                }
                break;
            default:break;
        }

        setTitleBarLeftText("取消");
        setTitleBarRightText("保存");

        ivClear = (ImageView) findViewById(R.id.iv_clear);
        ivClear.setOnClickListener(v -> etContent.setText(""));

    }

    @Override
    protected void onRightClickListener(View v) {
        String content = etContent.getText().toString().trim();

        if ("0".equals(type) &&  TextUtils.isEmpty(content)) {
            showToast("请输入姓名");
            return;
        } else if ("1".equals(type)) {
            if (TextUtils.isEmpty(content)) {
                showToast("请输入身份证件号");
                return;
            } else if (!Utils.isEmpty(Utils.IDCardValidate(etContent.getText().toString().trim()))) {
                showToast(Utils.IDCardValidate(etContent.getText().toString().trim()));
                return;
            }
        } else if ("2".equals(type) &&  TextUtils.isEmpty(content)){
            showToast("请输入职业");
            return;
        }
        hideKeyboard();
        Intent intent = new Intent();
        if ("0".equals(type)) {
            intent.putExtra("realName", etContent.getText().toString().trim());
        } else if ("1".equals(type)) {
            intent.putExtra("cardNumber", etContent.getText().toString().trim());
        } else {
            intent.putExtra("occupation", etContent.getText().toString().trim());
        }
        setResult(RESULT_OK, intent);
        finish();
    }
}
